################################################################################
# Create a string of numbers that will be used to insert anonymous lenders based
# on the anonymous lenders in each HTML file.
# 
# This assumes that a directory is full of individual loan page HTML files.
################################################################################

use strict;
use File::Find;


my $loanDirectory = "D:/Backups and Exports/Loan HTML/";
my $numberOfAnonymousLenders = 0;
my $numberOfFiles = 0;
my @loanIDs;
my $loanIDsCommaSeparatedList = "";


#Count lenders in each file
my @directories;
@directories = ($loanDirectory);
find(\&parseLoan, @directories);


printLoans();
print "\n\n\nTotal number of anonymous lenders = ".$numberOfAnonymousLenders."\n";




################################################################################
sub parseLoan
{
  my $fileName = $_;

  #Only process file names
  if($fileName == ".")
  {
    return;
  }

  #The file name is LOANID.html, so remove the .html part
  my $loanID = $fileName;
  $loanID =~ s/\.html//;
  
  #TEST
  #my $numberOfLendersPerFile = 0;
  $numberOfFiles++;

  #Used for testing...
  #if($numberOfFiles > 200)
  #{
  #  printLoans();
  #  print "\n\n\nTotal number of anonymous lenders = ".$numberOfAnonymousLenders."\n";
  #  exit;
  #}

  
  #Process any files that end in .html
  if(lc($fileName) =~ /.*\.html$/)
  {
    open FILE, "<", $fileName or die $!;
    
    #Go through each line, look for "Anonymous" and count it
    while(my $line = <FILE>)
    {
      if ($line =~ m/Anonymous/)
      {
        $numberOfAnonymousLenders++;
        $loanIDsCommaSeparatedList = $loanIDsCommaSeparatedList.",".$loanID;
        push @loanIDs, $loanID;

        #TEST
        #$numberOfLendersPerFile++;
      }
    }
  }
}




#Print comma separated list of loan IDs.
#Only print 100 per line (a single large line may not work well in some editors)
sub printLoans
{
  for(my $i = 0; $i < @loanIDs; $i++)
  {
    if($i == 0)
    {
      print @loanIDs[$i];
    }
    else
    {
      #Print a newline every 100 elements
      if($i % 100 == 0)
      {
        print "\n";
      }
      print ",".@loanIDs[$i];
    }
  }
}


